// pages/reg/reg.js
Page({
  mixins: [require('../mixin/themeChanged')],

  /**
   * 页面的初始数据
   */
  data: {
    classone:["请选择"],
    classoneindex:"0",
    classtwo:["请选择"],
    classtwoindex:"0",
    classthree:["请选择"],
    classthreeindex:"0",
    thingorder:[],
    classindex:[0,0,0],
    num:"1",
    city:"",
    list: [
      {
          id: '1',
          name: '查看已领用信息',
          pages: [{sn:"测试会议",url:"../thing/thingorder"}]
      },
     ],

  },
  


  bindPost:function(e){ 
    var that=this
    var url = wx.getStorageSync("config") 
    var content=JSON.stringify(this.data.thingorder)
    console.log(this.data.thingorder)
    

    wx.showModal({
      title: '提交确认',
      content: '请核对领用清单内容是否正确',
      success: function(res) {
       if (res.confirm) {   
      
        wx.request({
          url: url.service.homeUrl+'/thing/thingorder',
          data: { 
            content:content,
            uid:wx.getStorageSync('uid'), 
            city:that.data.city,
            type:"LY"     
                },

          header: { 'content-type': 'application/x-www-form-urlencoded' },
          method: 'POST',
          dataType: 'json',
          responseType: 'text',
          success: (res) => {
           
            wx.navigateTo({url: '../thing/thingorder',})
            
            wx.showToast({
              title: res.data.msg,
              icon:"none"
            }) 
          },
          fail: function (res) {
            wx.showToast({
              title: res.data.msg,
            }) 
           },
          complete: function (res) { },
        })
       
       }
      }
     })
  },




 

  getThingNum:function(e){
    this.setData({
      num: e.detail.value
    }) 
  },


  bindClassoneChange: function(e) {
    var that=this
    var classindex=this.data.classindex
    var url = wx.getStorageSync("config") 
    classindex[0]=parseInt(e.detail.value)

    this.setData({
      classoneindex: e.detail.value,
      classtwo:[]
    }) 

    wx.request({
      url: url.service.homeUrl+'/thing/getclasstwo',
      data: {
               city:that.data.city,
               classone:that.data.classone[that.data.classoneindex]
            },

      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {

        for(var i=0;i<res.data.length;i++)
        { var newarray = [res.data[i].classtwo,];        
         that.data.classtwo = that.data.classtwo.concat(newarray);
         that.setData({
          classtwo: that.data.classtwo
        });  
      }},
      fail: function (res) {console.log(res) },
      complete: function (res) { console.log(res) },
    })
  },

  bindClasstwoChange: function(e) {
    var that=this
    var classindex=this.data.classindex
    var url = wx.getStorageSync("config") 
    classindex[0]=parseInt(e.detail.value)

    this.setData({
      classtwoindex: e.detail.value,
      classthree:[]
    }) 

    wx.request({
      url: url.service.homeUrl+'/thing/getclassthree',
      data: {
               city:that.data.city,
               classtwo:that.data.classtwo[that.data.classtwoindex]
            },
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {

        for(var i=0;i<res.data.length;i++)
        { var newarray = [res.data[i].classthree,];        
         that.data.classthree = that.data.classthree.concat(newarray);
         that.setData({
          classthree: that.data.classthree
        });  
      }},
      fail: function (res) {console.log(res) },
      complete: function (res) { console.log(res) },
    })
  },


  bindClassthreeChange: function(e) {
    this.setData({
      classthreeindex: e.detail.value
    })    
  },

  addThingOrder:function(e){
    var that =this
    var thingorder=[]
    var classone=this.data.classone[this.data.classoneindex]
    var classtwo=this.data.classtwo[this.data.classtwoindex]
    var classthree=this.data.classthree[this.data.classthreeindex]
    thingorder=this.data.thingorder

    thingorder.push({classone:classone,classtwo:classtwo,classthree:classthree,num:that.data.num})
    this.setData({
      thingorder:thingorder
    })
  },


  deleteThingOrder:function(e){
    var thingorder=[]
    thingorder=this.data.thingorder
    thingorder.splice(e.detail.value,1)
    this.setData({
      thingorder:thingorder
    })
  },

  kindToggle: function (e) {
    var id = e.currentTarget.id, list = this.data.list;
    for (var i = 0, len = list.length; i < len; ++i) {
        if (list[i].id == id) {
            list[i].open = !list[i].open
        } else {
            list[i].open = false
        }
    }
    this.setData({
        list: list
    });
},

changeTheme: function() {
    console.log(this.data);
    getApp().themeChanged(this.data.theme === 'light' ? 'dark' : 'light');
},





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    var url = wx.getStorageSync("config") 
    this.setData({
      city: wx.getStorageSync('city')
    })  

  
    wx.request({
      url: url.service.homeUrl+'/thing/getclassone',
      data: {city:that.data.city,
           
            },
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        for(var i=0;i<res.data.length;i++)
        { var newarray = [res.data[i].classone,];        
         that.data.classone = that.data.classone.concat(newarray);
         that.setData({
          classone: that.data.classone
        });  
      }},
      fail: function (res) { },
      complete: function (res) { },
    })



    var thing='list[0].pages'
    wx.request({     
      url: url.service.homeUrl+'/thing/getthingorderlist',
      data: { 
              uid:wx.getStorageSync('uid'),
              type:"LY"
            },

      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        
        this.setData({[thing]: res.data})
        console.log(that.data.list)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})